package com.sugar.subscribe.course.module.business.service.coachservice;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.subscribe.course.module.business.controller.admin.coachservice.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachservice.CoachServiceDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.coachservice.CoachServiceMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 教练服务 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class CoachServiceServiceImpl implements CoachServiceService {

    @Resource
    private CoachServiceMapper coachServiceMapper;

    @Override
    public Long createCoachService(CoachServiceSaveReqVO createReqVO) {
        // 插入
        CoachServiceDO coachService = BeanUtils.toBean(createReqVO, CoachServiceDO.class);
        coachServiceMapper.insert(coachService);

        // 返回
        return coachService.getId();
    }

    @Override
    public void updateCoachService(CoachServiceSaveReqVO updateReqVO) {
        // 校验存在
        validateCoachServiceExists(updateReqVO.getId());
        // 更新
        CoachServiceDO updateObj = BeanUtils.toBean(updateReqVO, CoachServiceDO.class);
        coachServiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCoachService(Long id) {
        // 校验存在
        validateCoachServiceExists(id);
        // 删除
        coachServiceMapper.deleteById(id);
    }

    @Override
        public void deleteCoachServiceListByIds(List<Long> ids) {
        // 删除
        coachServiceMapper.deleteByIds(ids);
        }


    private void validateCoachServiceExists(Long id) {
        if (coachServiceMapper.selectById(id) == null) {
            throw exception(COACH_SERVICE_NOT_EXISTS);
        }
    }

    @Override
    public CoachServiceDO getCoachService(Long id) {
        return coachServiceMapper.selectById(id);
    }

    @Override
    public PageResult<CoachServiceDO> getCoachServicePage(CoachServicePageReqVO pageReqVO) {
        return coachServiceMapper.selectPage(pageReqVO);
    }

}